﻿using UnityEngine;
using UnityEngine.Networking;
using Sa1;

public class Bullet : MonoBehaviour {

    public float speed;
    public float lifeTime;

    public GameObject thePlayer;

    public readonly Container container = new Container("[Bullets]");

    // private float _spawnTime;
    private bool activep = false;

    void Start() {
        // _spawnTime = Time.time;
        container.set(this.gameObject);
        Destroy(this.gameObject, lifeTime);
    }

    void Update() {
        transform.Translate(1 * Time.deltaTime * speed, 0, 0);
        // if (Time.time - _spawnTime > lifeTime) {
        //     Destroy(this.gameObject);
        // }
    }

    void OnTriggerEnter2D(Collider2D coll) {
        if (!activep) return;
        Destroy(this.gameObject);
        var p = coll.transform.parent;
        if (p != null) {
            p.gameObject.SendMessage("takeDamage", 1, SendMessageOptions.DontRequireReceiver);
        }
    }

    void OnTriggerExit2D(Collider2D coll) {
        activep = true;
    }
}
